#include "addfrienddialog.h"

FriendResultItem::FriendResultItem(const UserInfo &userInfo, QWidget* parent):
    QWidget(parent),m_userInfo(userInfo)
{
    // 1. 设置基本属性
    this->setFixedHeight(50);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    this->setStyleSheet("QWidget:hover { background-color: #D9D8D8; }"
                        "QLabel { background-color:transparent; }");

    // 2. 创建核⼼布局
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(10, 0, 10, 0);
    layout->setSpacing(5);
    this->setLayout(layout);
    // 3. 创建头像
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(35, 35);
    avatarBtn->setIconSize(QSize(35, 35));
    avatarBtn->setIcon(userInfo.m_avatar);
    // 4. 创建名字
    QLabel* nameLabel = new QLabel(userInfo.m_nickName);
    nameLabel->setFixedHeight(25);
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    nameLabel->setStyleSheet("QLabel { background-color:transparent; font-size: 14px; font-weight: 600;}");
    // 5. 创建签名
    QLabel* signatureLabel = new QLabel(userInfo.m_description);
    signatureLabel->setFixedHeight(25);
    signatureLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    signatureLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    // 6. 创建添加按钮
    m_addBtn = new QPushButton("添加好友");
    m_addBtn->setFixedSize(70, 20);
    m_addBtn->setStyleSheet("QPushButton { border: none; background-color:rgb(137, 217, 97); color: white; border-radius: 10px; }"
                            "QPushButton:pressed { background-color: rgb(198, 198, 198); }");

    // 7. 添加到布局管理器中
    layout->addWidget(avatarBtn, 0, 0, 2, 1);
    layout->addWidget(nameLabel, 0, 1, 1, 1);
    layout->addWidget(signatureLabel, 1, 1, 1, 1);
    layout->addWidget(m_addBtn, 0, 2, 2, 1);


    connect(m_addBtn, &QPushButton::clicked, this, [=]{
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        dataCenter->addFriendApplyAsync(m_userInfo.m_userId);

        // 更改一下按钮状态
        m_addBtn->setEnabled(false);
        m_addBtn->setText("已申请");
        m_addBtn->setStyleSheet("QPushButton { border: none; background-color: rgb(198, 198, 198); color: white; border-radius: 10px; }");
    });
}

void FriendResultItem::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

AddFriendDialog::AddFriendDialog(QWidget *parent)
{
    this->setFixedSize(300, 400);
    this->setWindowTitle("用户搜索");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QWidget { background-color: white; }"
                        "QScrollArea { border: none; }"
                        "QScrollBar:vertical { width: 2px; background-color: white; }"
                        "QScrollBar::handle:vertical {background-color: rgb(205, 205, 205);}"
                        "QScrollBar:horizontal { height: 0px;}"
                        "QLineEdit { border-radius: 10px; background-color: #F3F3F3; border: none; padding-left: 10px; font-size: 12px;} ");

    // 1.网格布局
    QGridLayout* gLayout = new QGridLayout();
    gLayout->setContentsMargins(10, 10 ,10, 10);
    gLayout->setHorizontalSpacing(5);
    gLayout->setAlignment(Qt::AlignTop | Qt::AlignVCenter);
    this->setLayout(gLayout);

    // 2.搜索栏 + 搜索
    m_searchEdit = new QLineEdit();
    m_searchEdit->setFixedHeight(25);
    m_searchEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    m_searchEdit->setPlaceholderText("按手机号/用户序号查找");
    QPushButton* searchBtn = new QPushButton();
    searchBtn->setIcon(QIcon(":/resource/image/search.png"));
    searchBtn->setFixedSize(25, 25);
    searchBtn->setIconSize(QSize(20, 20));
    searchBtn->setStyleSheet("QPushButton { background-color: rgb(243, 243, 243);border: none; border-radius: 5px;}"
                             "QPushButton:hover {background-color: rgb(219, 219, 219);} "
                             "QPushButton:pressed {background-color: rgb(206, 206, 206);}");
    gLayout->addWidget(m_searchEdit, 0, 0, 1, 8);
    gLayout->addWidget(searchBtn, 0, 8, 1, 1);

    // 3.搜索结果滚动区
    QScrollArea* resultArea = new QScrollArea();
    resultArea->setWidgetResizable(true);
    resultArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_resultContainer = new QWidget();
    resultArea->setWidget(m_resultContainer);
    // 3.1滚动区容器里垂直布局
    QVBoxLayout* vLayout = new QVBoxLayout(m_resultContainer);
    vLayout->setContentsMargins(0, 0, 0, 0);
    vLayout->setSpacing(0);
    gLayout->addWidget(resultArea, 1, 0, 1, 9);

#if TEST_UI
    for(int i = 0; i < 15; ++i){
        UserInfo* user = new UserInfo();
        user->m_nickName = "老六";
        user->m_description = "hhhhhhhhhh";
        user->m_avatar = QIcon(":/resource/image/defaultAvatar2.jpg");
        addResult(*user);
    }
#endif

    // 4. 绑定信号
    connect(searchBtn, &QPushButton::clicked, this, &AddFriendDialog::clickAddBtn);
}

void AddFriendDialog::addResult(const UserInfo &userInfo)
{
    FriendResultItem* item = new FriendResultItem(userInfo);
    m_resultContainer->layout()->addWidget(item);
}

void AddFriendDialog::clear()
{
    QVBoxLayout* vlayout = dynamic_cast<QVBoxLayout*>(m_resultContainer->layout());
    QLayoutItem* child = nullptr;
    while((child = vlayout->itemAt(0))!= nullptr){
        vlayout->removeItem(child);
        delete child->widget();
        delete child;
    }
}

void AddFriendDialog::clickAddBtn()
{
    QString searchKey = m_searchEdit->text().trimmed();
    if(searchKey.isEmpty()){
        LOG() << "搜索内容为空";
        return ;
    }
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter, &model::DataCenter::searchUserDone, this, &AddFriendDialog::clickSearchBtnDone, Qt::UniqueConnection);
    dataCenter->searchUserAsync(searchKey);
    LOG() << "搜索内容：" << searchKey;
}

void AddFriendDialog::clickSearchBtnDone()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    if(dataCenter->getSearchUserResult() == nullptr){
        LOG() << "搜索内容为空";
        return ;
    }
    clear();
    for(const auto& user : *dataCenter->getSearchUserResult()){
        addResult(user);
    }
}
